<?php

namespace App\Http\Controllers;

use App\Common\Constant\Status;
use App\Common\Constant\Params;
use App\Services\ICookieService;
use App\Services\IMasterService;
use App\Services\ISessionService;
use App\Services\IWeixinService;
use Illuminate\Http\Request;

/**
 * 用户权限
 * Class AuthController
 * @package App\Http\Controllers
 */
class AuthController extends BaseController
{
    
    /**
     * 注册
     * @param Request $request
     */
    public function register(Request $request,ISessionService $sessionService,IMasterService $masterService,ICookieService $cookieService){
        /**
         * 检查微信用户
         */
        $weixinUserId = $cookieService->currentWeixinUserid();
        if (empty($weixinUserId)){
            return redirect(route('getWeixinUser',['redirectUrl'=>url()->full()]));
        }

        /**
         * 检查微信用户登录
         */
//        $weixinUserinfo = $sessionService->getCurrentWeixinUserinfo();
//        if (empty($weixinUserinfo)){
//            return redirect(route('getWeixinUserinfo',['redirectUrl'=>url()->full()]));
//        }

        $phoneNum = $cookieService->verifiedPhone();
        if (!$phoneNum){
            return redirect(route('smsVerify',['redirectUrl'=>url()->full()]));
        }

        $redirectUrl = route('registerSuccess',['redirectUrl'=>$sessionService->getRedirectUrl('autologin')]);
        return view('auth/register',['weixinUserId'=>$weixinUserId,'phoneNum'=>$phoneNum,'redirectUrl'=>$redirectUrl]);
    }

    /**
     * 注册成功
     * @param Request $request
     */
    public function registerSuccess(Request $request,ISessionService $sessionService,IMasterService $masterService,ICookieService $cookieService){
        /**
         * 检查平台用户
         */
        $user = $sessionService->getCurrentUser();
        if (empty($user)){
            $userid = $cookieService->currentUserid();
            $weixinUserid = $cookieService->currentWeixinUserid();
            if (empty($userid) && empty($weixinUserid)){//注册失败
                return $this->renderFail(Status::USER_REGISTER_WRONG);
            }else if ($userid){
                $user = $masterService->getUserByUserid($userid);
            }else if ($weixinUserid){
                $user = $masterService->getUserByWeixinUserid($weixinUserid);
            }
            if (empty($user)){//注册失败
                return $this->renderFail(Status::USER_REGISTER_WRONG);
            }
        }

        if (empty($user->weixin_userid) || empty($user->weixin_openid)){
            /**
             * 检查微信用户
             */
            $weixinUserId = $cookieService->currentWeixinUserid();
            if (empty($weixinUserId)){
                return redirect(route('getWeixinUser',['redirectUrl'=>url()->full()]));
            }
            if ($masterService->updateWeinxinUserid($user->id,$weixinUserId)){
                $user = $sessionService->getCurrentUser();
            }
        }
        if (empty($user->weixin_userid)){
            return $this->renderFail(Status::WEIXIN_USERID_MISS);
        }

        if (!$sessionService->setCurrentUser($user)){
            return $this->renderFail(Status::SESSION_SETTING_ERROR);
        }
        return view('auth/registerSuccess');
    }
    
    /**
     * 自动登录
     * @param Request $request
     */
    public function autoLogin(Request $request,IWeixinService $weixinService,IMasterService $masterService,ISessionService $sessionService,ICookieService $cookieService){
        /**
         * 检查微信用户
         */
        $weixinUserId = $cookieService->currentWeixinUserid();
        if (empty($weixinUserId)){
            return redirect(route('getWeixinUser',['redirectUrl'=>url()->full()]));
        }

        /**
         * 检查平台用户
         */
        $user = $sessionService->getCurrentUser();
        if (empty($user)){
            $user = $masterService->getUserByWeixinUserid($weixinUserId);
            if (empty($user)){//未在数据库中注册
                return redirect(route('register',['redirectUrl'=>url()->full()]));
            }
            if (!$sessionService->setCurrentUser($user)){
                return $this->renderFail(Status::SESSION_SETTING_ERROR);
            }
        }
        $userid = $cookieService->currentUserid();
        if (empty($userid) || ($userid != $user->id)){
            if (!$cookieService->currentUserid($user->id)){
                return $this->renderFail(Status::COOKIE_SETTING_ERROR);
            }
        }

        /**
         * 更新微信userid和openid
         */
        if (empty($user->weixin_userid) || empty($user->weixin_openid)){
            $masterService->updateWeinxinUserid($user->id,$weixinUserId);
        }

        return redirect($sessionService->getRedirectUrl('/'));//跳转到之前页
    }
    
    /**
     * 短信验证
     * @param Request $request
     */
    public function smsVerify(Request $request,ISessionService $sessionService){
        return view('auth.smsVerify',['redirectUrl'=>$sessionService->getRedirectUrl('register')]);
    }

    /**
     * 重新登录
     * @param Request $request
     */
    public function logout(Request $request,ISessionService $sessionService){
        $sessionService->logout();
        return redirect('/');
    }
}
